/*
 * Copyright 2008-2009 Aying.Net Powered by Fuchun.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package net.aying.util;

import java.math.RoundingMode;

import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;

import net.aying.util.NumberUtils;

/**
 * NumberUtils 类的 JUnit 单元测试。
 * 
 * @author Fuchun
 * @version 1.0, 2009-6-14
 */
public class NumberUtilsTestCase extends UtilBasedTestCase {

	private static final byte BYTE_ZERO = (byte) 0x0;

	private static final short SHORT_ZERO = (short) 0x0;

	private static final int INT_ZERO = 0;

	private static final long LONG_ZERO = 0L;

	private static final float FLOAT_ZERO = 0.0f;

	private static final double DOUBLE_ZERO = 0.0d;

	private static final String NUMBER_STRING_POSTFIX = "abc";

	private static final byte NORMAL_BYTE = 100;

	private static final short NORMAL_SHORT = 10000;

	private static final int NORMAL_INTEGER = 1000000000;

	private static final long NORMAL_LONG = 1000000000000000000L;

	private static final float NORMAL_FLOAT = 1.0E38f;

	private static final double NORMAL_DOUBLE = 1.0E308d;

	private String bString1, bString2, sString1, sString2, iString1, iString2, lString1, lString2, fString1, fString2,
			dString1, dString2;

	private void initTestNumber() {
		bString1 = String.valueOf(NORMAL_BYTE);
		bString2 = String.valueOf(NORMAL_BYTE).concat(NUMBER_STRING_POSTFIX);
		sString1 = String.valueOf(NORMAL_SHORT);
		sString2 = String.valueOf(NORMAL_SHORT).concat(NUMBER_STRING_POSTFIX);
		iString1 = String.valueOf(NORMAL_INTEGER);
		iString2 = String.valueOf(NORMAL_INTEGER).concat(NUMBER_STRING_POSTFIX);
		lString1 = String.valueOf(NORMAL_LONG);
		lString2 = String.valueOf(NORMAL_LONG).concat(NUMBER_STRING_POSTFIX);
		fString1 = String.valueOf(NORMAL_FLOAT);
		fString2 = String.valueOf(NORMAL_FLOAT).concat(NUMBER_STRING_POSTFIX);
		dString1 = String.valueOf(NORMAL_DOUBLE);
		dString2 = String.valueOf(NORMAL_DOUBLE).concat(NUMBER_STRING_POSTFIX);
	}

	private static String message(String str) {
		return "两个 " + str + " 值不相等！";
	}

	public NumberUtilsTestCase(String testName) {
		super(testName);
	}

	/* Setup */
	public void setUp() throws Exception {
		initTestNumber();
	}

	public void tearDown() throws Exception {

	}

	/**
	 * @see NumberUtils#parseByte(String)
	 * @see NumberUtils#parseByte(String, Byte)
	 */
	public void test_parseByte() throws Exception {
		assertEquals(message("byte"), NORMAL_BYTE, NumberUtils.parseByte(bString1));
		assertEquals(message("byte"), NORMAL_BYTE, NumberUtils.parseByte(bString1, NORMAL_BYTE));
		assertEquals(message("byte"), BYTE_ZERO, NumberUtils.parseByte(bString2));
		assertEquals(message("byte"), (byte) 127, NumberUtils.parseByte(bString2, (byte) 127));
		assertEquals("两个 Byte 对象不相等", Byte.valueOf(NORMAL_BYTE), NumberUtils.parseByte(bString2, Byte
				.valueOf(NORMAL_BYTE)));
	}

	/**
	 * @see NumberUtils#parseShort(String)
	 * @see NumberUtils#parseShort(String, Short)
	 */
	public void test_parseShort() throws Exception {
		assertEquals(message("short"), NORMAL_SHORT, NumberUtils.parseShort(sString1));
		assertEquals(message("short"), NORMAL_SHORT, NumberUtils.parseShort(sString1, NORMAL_SHORT));
		assertEquals(message("short"), SHORT_ZERO, NumberUtils.parseShort(sString2));
		assertEquals(message("short"), NORMAL_SHORT, NumberUtils.parseShort(sString2, NORMAL_SHORT));
		assertEquals("两个 Short 对象不相等", Short.valueOf(NORMAL_SHORT), NumberUtils.parseShort(sString2, Short
				.valueOf(NORMAL_SHORT)));
	}

	/**
	 * @see NumberUtils#parseInt(String)
	 * @see NumberUtils#parseInt(String, int)
	 */
	public void test_parseInt() throws Exception {
		assertEquals(message("int"), NORMAL_INTEGER, NumberUtils.parseInt(iString1));
		assertEquals(message("int"), NORMAL_INTEGER, NumberUtils.parseInt(iString1, NORMAL_INTEGER));
		assertEquals(message("int"), INT_ZERO, NumberUtils.parseInt(iString2));
		assertEquals(message("int"), NORMAL_INTEGER, NumberUtils.parseInt(iString2, NORMAL_INTEGER));
		assertEquals("两个 Integer 对象不相等", Integer.valueOf(NORMAL_INTEGER), NumberUtils.parseInt(iString2, Integer
				.valueOf(NORMAL_INTEGER)));
	}

	/**
	 * @see NumberUtils#parseLong(String)
	 * @see NumberUtils#parseLong(String, Long)
	 */
	public void test_parseLong() throws Exception {
		assertEquals(message("long"), NORMAL_LONG, NumberUtils.parseLong(lString1));
		assertEquals(message("long"), NORMAL_LONG, NumberUtils.parseLong(lString1, NORMAL_LONG));
		assertEquals(message("long"), LONG_ZERO, NumberUtils.parseLong(lString2));
		assertEquals(message("long"), NORMAL_LONG, NumberUtils.parseLong(lString2, NORMAL_LONG));
		assertEquals("两个 Long 对象不相等", Long.valueOf(NORMAL_LONG), NumberUtils.parseLong(lString2, Long
				.valueOf(NORMAL_LONG)));
	}

	/**
	 * @see NumberUtils#parseFloat(String)
	 * @see NumberUtils#parseFloat(String, float)
	 */
	public void test_parseFloat() throws Exception {
		assertEquals(message("float"), NORMAL_FLOAT, NumberUtils.parseFloat(fString1));
		assertEquals(message("float"), NORMAL_FLOAT, NumberUtils.parseFloat(fString1, NORMAL_FLOAT));
		assertEquals(message("float"), FLOAT_ZERO, NumberUtils.parseFloat(fString2));
		assertEquals(message("float"), NORMAL_FLOAT, NumberUtils.parseFloat(fString2, NORMAL_FLOAT));
		assertEquals("两个 Float 对象不相等", Float.valueOf(NORMAL_FLOAT), NumberUtils.parseFloat(fString2, Float
				.valueOf(NORMAL_FLOAT)));
	}

	/**
	 * @see NumberUtils#parseDouble(String)
	 * @see NumberUtils#parseDouble(String, double)
	 */
	public void test_parseDouble() throws Exception {
		assertEquals(message("double"), NORMAL_DOUBLE, NumberUtils.parseDouble(dString1));
		assertEquals(message("double"), NORMAL_DOUBLE, NumberUtils.parseDouble(dString1, NORMAL_DOUBLE));
		assertEquals(message("double"), DOUBLE_ZERO, NumberUtils.parseDouble(dString2));
		assertEquals(message("double"), NORMAL_DOUBLE, NumberUtils.parseDouble(dString2, NORMAL_DOUBLE));
		assertEquals("两个 Double 对象不相等", Double.valueOf(NORMAL_DOUBLE), NumberUtils.parseDouble(fString2, Double
				.valueOf(NORMAL_DOUBLE)));
	}

	/** @see NumberUtils#buildIntegerArray(int, int, int...) */
	public void test_buildIntegerArray() throws Exception {
		Integer[] nums1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		Integer[] nums2 = NumberUtils.buildIntegerArray(0, 10, 1);
		for (int i = 0, len = nums1.length; i < len; i++) {
			assertEquals(nums1[i], nums2[i]);
		}
	}

	/** @see NumberUtils#equals(Number, Number) */
	public void test_equals() throws Exception {
		byte b = 100;
		short s = 10000;
		int i = 1000000000;
		long l = 1000000000000000000L;
		float f = 1.0E38f;
		double d = 1.0E308d;
		assertTrue(NumberUtils.equals(b, NORMAL_BYTE));
		assertTrue(NumberUtils.equals(s, NORMAL_SHORT));
		assertTrue(NumberUtils.equals(i, NORMAL_INTEGER));
		assertTrue(NumberUtils.equals(l, NORMAL_LONG));
		assertTrue(NumberUtils.equals(f, NORMAL_FLOAT));
		assertTrue(NumberUtils.equals(d, NORMAL_DOUBLE));
	}

	/**
	 * @see NumberUtils#format(Number)
	 * @see NumberUtils#format(Number, String, RoundingMode...)
	 */
	public void test_format() {
		float pi = 3.1415926f;
		assertEquals("3", NumberUtils.format(pi));
		assertEquals("3.14", NumberUtils.format(pi, NumberUtils.TWO_SCALE_PATTERN));
		assertEquals("3.1416", NumberUtils.format(pi, "###0.0000"));
		assertEquals("3.1415", NumberUtils.format(pi, "###0.0000", RoundingMode.DOWN));
	}

	public static Test suite() {
		return new TestSuite(NumberUtilsTestCase.class);
	}

	public static void main(String[] args) {
		TestRunner.run(suite());
	}
}
